#!/usr/bin/python3
# -*- coding: UTF-8 -*-


class Student:
    "所有学生的基类"
    stuCount = 0

    def __init__(self, name, age):
        self.name = name
        self.age = age
        Student.stuCount += 1
    
    def __del__(self):
      class_name = self.__class__.__name__
      print (class_name, "销毁")

    def displayCount(self):
        print("Total  %d" % Student.stuCount)

    def display(self):
        print("Name : ", self.name, ", Salary: ", self.age)


stu = Student("zmo", 18)
stu.displayCount()
stu.display()

hasattr(stu, "age")  # 如果存在 'age' 属性返回 True。
getattr(stu, "age")  # 返回 'age' 属性的值
setattr(stu, "sex", "man")  # 添加属性 'sex' 值为 8
print(getattr(stu, "sex"))  # 返回 'age' 属性的值
delattr(stu, "sex")  # 删除属性 'age'

try:
    print(getattr(stu, "sex"))  # 返回 'age' 属性的值
except :
    print("error:getattr sex")
else:
    print("success")
finally:
    print("finally")
del stu

    
print ("Student.__doc__:", Student.__doc__)
print ("Student.__name__:", Student.__name__)
print ("Student.__module__:", Student.__module__)
print ("Student.__bases__:", Student.__bases__)
print ("Student.__dict__:", Student.__dict__)



 
print(" ===== Python 类的继承 ===== ")


class Parent:        # 定义父类
    parentAttr = 100
    __privateValue = 999 

    def __init__(self):
        print("调用父类构造函数")

    def parentMethod(self):
        print('调用父类方法',self.__privateValue)

    def setAttr(self, attr):
        Parent.parentAttr = attr

    def getAttr(self):
        print("父类属性 :", Parent.parentAttr)

    def myMethod(self):
        print('调用 Parent myMethod 方法')


class Child(Parent):  # 定义子类
    def __init__(self):
        super().__init__()  # 调用父类的构造函数，确保父类的初始化逻辑执行
        print("调用子类构造方法")

    def childMethod(self):
        print('调用子类方法')

    def myMethod(self):
        print('调用  Child myMethod 方法')


c = Child()          # 实例化子类
c.childMethod()      # 调用子类的方法
c.parentMethod()     # 调用父类方法
c.setAttr(200)       # 再次调用父类的方法 - 设置属性值
c.getAttr()          # 再次调用父类的方法 - 获取属性值


parent = Parent()          # 实例化子类
print("parent.__privateValue:",parent._Parent__privateValue)